﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using NorthwindModel;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class NorthwindService
{
    // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
    // To create an operation that returns XML,
    //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
    //     and include the following line in the operation body:
    //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
    [OperationContract]
    public void DoWork()
    {
        // Add your operation implementation here
        return;
    }

    // Add more operations here and mark them with [OperationContract]

    [OperationContract]
    public List<CustomersInfo> GetAllCustomers()
    {
        List<CustomersInfo> result = null;
        using (NorthwindEntities context = new NorthwindEntities())
        {
            result = context.Customers.Select(customer => new CustomersInfo()
            {
                CustomerID = customer.CustomerID,
                CompanyName = customer.CompanyName,
                ContactName = customer.ContactName
            }).ToList();
        }
        return result;
    }

    [OperationContract]
    public List<OrdersInfo> GetCustomerOrders(string customerID)
    {
        List<OrdersInfo> result = null;
        using (NorthwindEntities context = new NorthwindEntities())
        {
            result = context.Orders.Where(order => order.CustomerID == customerID).Select(order => new OrdersInfo()
            {
                OrderID = order.OrderID,
                OrderDate = order.OrderDate.HasValue ? order.OrderDate.Value : DateTime.MinValue,
                ShipCountry = order.ShipCountry
            }).ToList();
        }
        return result;
    }

}


[DataContract]
public class CustomersInfo
{
    [DataMember]
    public string CustomerID { get; set; }
    [DataMember]
    public string CompanyName { get; set; }
    [DataMember]
    public string ContactName { get; set; }
}

[DataContract]
public class OrdersInfo
{
    [DataMember]
    public int OrderID { get; set; }
    [DataMember]
    public DateTime OrderDate { get; set; }
    [DataMember]
    public string ShipCountry { get; set; }
}